<?php

function register_my_menus()
{
    register_nav_menus(
        array('header-menu' => __('Menu'))
    );
}

add_action('init', 'register_my_menus');

/*
function fix_title()
{
    if ($_POST['post_type'] == "pitches") {
        $posts = get_field('venue');
        if( $posts ):
            foreach( $posts as $post_object):
                $name = get_field('name', $post_object->ID);
            endforeach;
        endif;

        $_POST['post_title'] = get_field('pid') . ' - ' . $name;
    }
    return $_POST['post_title'];
}
add_filter('title_save_pre', 'fix_title');
*/

add_action( 'wp_print_styles', 'my_deregister_styles', 100 );

function my_deregister_styles() {
    wp_deregister_style( 'wp-admin' );
}

function custom_post_type_title($post_id)
{
    $post_id;
    global $wpdb;
    if (get_post_type($post_id) == 'pitches') {
        $name = get_field('pid');

        $posts = get_field('venue');
        if ($posts):
            foreach ($posts as $post_object):
                $name = $name . ' - ' . get_field('name', $post_object->ID);
            endforeach;
        endif;
        $where = array('ID' => $post_id);
        $slug = sanitize_title( $name );
        $wpdb->update($wpdb->posts, array('post_name' => $slug), $where);
        $wpdb->update($wpdb->posts, array('post_title' => $name), $where);
    }
    if (get_post_type($post_id) == 'slots') {
        $name = get_field('start_time').' - '.get_field('end_time');

        $posts = get_field('pitch');
        if ($posts):
            foreach ($posts as $post_object):
                $name = $name . ' - ' . get_field('pid', $post_object->ID);

                $posts = get_field('venue', $post_object->ID);
                if ($posts):
                    foreach ($posts as $post_object2):
                        $name = $name . ' - ' . get_field('name', $post_object2->ID);
                    endforeach;
                endif;
            endforeach;
        endif;
        $where = array('ID' => $post_id);
        $slug = sanitize_title( $name );
        $wpdb->update($wpdb->posts, array('post_name' => $slug), $where);
        $wpdb->update($wpdb->posts, array('post_title' => $name), $where);
    }
    if (get_post_type($post_id) == 'bookings') {
        $date = DateTime::createFromFormat('Ymd', get_field('date'));
        if (isset($date)) {
            $name = $date->format('l, d M Y');
        }


        $posts = get_field('slot');
        if ($posts):
            foreach ($posts as $post_object):
                $name = $name . ' - ' . get_the_title($post_object->ID);
            endforeach;
        endif;
        $where = array('ID' => $post_id);
        $slug = sanitize_title( $name );
        $wpdb->update($wpdb->posts, array('post_name' => $slug), $where);
        $wpdb->update($wpdb->posts, array('post_title' => $name), $where);
    }
}

//add_action('save_pitches', 'custom_post_type_title');
//add_action('save_pitch', 'custom_post_type_title');
//add_action('save_post', 'custom_post_type_title');
//add_action('new_pitches', 'custom_post_type_title');
//add_action('new_pitch', 'custom_post_type_title');
//add_action('new_post', 'custom_post_type_title');
//add_action('transition_post_status', 'custom_post_type_title', 10, 3);
add_action('acf_save_post', 'custom_post_type_title', 20);

function myplugin_update_slug( $data, $postarr ) {
    if ( !in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) ) {
        $data['post_name'] = sanitize_title( $data['post_title'] );
    }
    return $data;
}
add_filter( 'wp_insert_post_data', 'myplugin_update_slug', 99, 2 );

function get_user_role()
{
    global $current_user;

    $user_roles = $current_user->roles;
    $user_role = array_shift($user_roles);

    return $user_role;
}

function get_user_id()
{
    global $current_user;
    return $current_user->ID;
}

function get_venue_name()
{
    $posts = get_field('venue');

    if ($posts):
        foreach ($posts as $post):
            setup_postdata($post);
            $temp = get_field('name',$posts[0]->ID);
        endforeach;
        wp_reset_postdata();
    endif;

    return $temp;
}

function get_venue_data($field)
{
    $posts = get_field('venue');

    if ($posts):
        foreach ($posts as $post):
            setup_postdata($post);
            $temp = get_field($field,$posts[0]->ID);
        endforeach;
        wp_reset_postdata();
    endif;

    return $temp;
}

function get_relation_data($relation,$field)
{
    $posts = get_field($relation);

    if ($posts):
        foreach ($posts as $post):
            setup_postdata($post);
            $temp = get_field($field,$posts[0]->ID);
        endforeach;
        wp_reset_postdata();
    endif;

    return $temp;
}

function get_user_relation_data($relation,$field,$user)
{
    $posts = get_field($relation,'user_'.$user);

    if ($posts):
        foreach ($posts as $post):
            setup_postdata($post);
            $temp = get_field($field,$posts[0]->ID);
        endforeach;
        wp_reset_postdata();
    endif;

    return $temp;
}

function get_any_relation_data($relation,$field,$id)
{
    $posts = get_field($relation,$id);

    if ($posts):
        foreach ($posts as $post):
            setup_postdata($post);
            $temp = get_field($field,$posts[0]->ID);
        endforeach;
        wp_reset_postdata();
    endif;

    return $temp;
}

function get_any_relation_id($relation,$id)
{
    $posts = get_field($relation,$id);

    if ($posts):
        foreach ($posts as $post):
            setup_postdata($post);
            $temp = $posts[0]->ID;
        endforeach;
        wp_reset_postdata();
    endif;

    return $temp;
}

function run_relation_function($relation,$function)
{
    $posts = get_field($relation);

    if ($posts):
        foreach ($posts as $post):
            setup_postdata($post);
            $function();
        endforeach;
        wp_reset_postdata();
    endif;
}

function printPitchDetail($label, $name)
{
    if (get_field($name)) {
        echo '<h3>' . $label . '</h3><p>' . get_field($name) . '</p>';
    }
}

function printVenueDetail($label, $name)
{
    if (get_field($name)) {
        echo '<div class="control-group"><h3 style="float:left;margin-top:0">' . $label . '</h3><div id="' . $name . '"class="controls">' . get_field($name) . '</div></div>';
    }
}

if (!current_user_can('administrator')):
  show_admin_bar(false);
endif;

